home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / OOPTUT34.ZIP / FIGDEMO.PAS < prev    next >
Pascal/Delphi Source File  |  1993-06-01  |  7KB  |  227 lines

  1. program FigureDemo;
  2.  
  3. {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  4. { Original Copyright (c) 1989 by Borland Interational, Inc.            }
  5. { From P-47 of the Object-Oriented Programming Guide, version 5.5      }
  6. { Extending FIGURES.PAS with type Arc.                                 }
  7. { Types FullLine  and Rectangle added by R. Shaw  3.4.90  & 9.2.91     }
  8. {    FIGDEMO.PAS  ->  FIGDEMO.EXE                                      }
  9. {______________________________________________________________________}
  10.  
  11. uses Crt, DOS, Graph, Figures, GraphPRN;
  12.  
  13. type
  14.   Arc = object (Circle)
  15.     StartAngle, EndAngle : Integer;
  16.     constructor Init(InitX, InitY : Integer;
  17.                      InitRadius : Integer;
  18.                      InitStartAngle, InitEndAngle : Integer);
  19.     procedure Show; virtual;
  20.     procedure Hide; virtual;
  21.   end;
  22.  
  23.   FullLine = object(Point)
  24.     XLength, YLength  : integer;
  25.     constructor Init(InitX, InitY  : integer;
  26.                      InitXLength, InitYLength : integer);
  27.     procedure Show; virtual;
  28.     procedure Hide; virtual;
  29.   end;
  30.  
  31.   Rectangle = object(FullLine)
  32.     constructor Init(InitX, InitY  : integer;
  33.                      InitXLength, InitYLength : integer);
  34.     procedure Show; virtual;
  35.     procedure Hide; virtual;
  36.   end;
  37.  
  38. var
  39.   GraphDriver : Integer;
  40.   GraphMode   : Integer;
  41.   ErrorCode   : Integer;
  42.   AnArc       : Arc;
  43.   ACircle     : Circle;
  44.   AFullLine   : FullLine;
  45.   ARectangle  : Rectangle;
  46.   APoint      : Point;
  47.   Reply       : char;
  48.   KeepColor   : word;
  49.   x1,x2       : integer;
  50.  
  51. {--------------------------------------------------------}
  52. { Arc's method declarations:                             }
  53. {--------------------------------------------------------}
  54.  
  55. constructor Arc.Init(InitX,InitY : Integer;
  56.                      InitRadius : Integer;
  57.                      InitStartAngle, InitEndAngle : Integer);
  58. begin
  59.   Circle.Init(InitX, InitY, InitRadius);
  60.   StartAngle := InitStartAngle;
  61.   EndAngle   := InitEndAngle;
  62. end;
  63.  
  64. procedure Arc.Show;
  65. begin
  66.   Visible := True;
  67.   Graph.Arc(X, Y, StartAngle, EndAngle, Radius);
  68. end;
  69.  
  70. procedure Arc.Hide;
  71. var
  72.   TempColor : Word;
  73. begin
  74.   TempColor := Graph.GetColor;
  75.   Graph.SetColor(GetBkColor);
  76.   Visible := False;
  77.   { Draw the arc in the background color to hide it }
  78.   Graph.Arc(X, Y, StartAngle, EndAngle, Radius);
  79.   SetColor(TempColor);
  80. end;
  81.  
  82. {-------------------------------------------------------}
  83. {  FullLine's method implementation                     }
  84. {-------------------------------------------------------}
  85.  
  86.    constructor FullLine.Init(InitX, InitY : Integer;
  87.                              InitXLength, InitYLength : Integer);
  88.  
  89.    begin
  90.       Point.Init(InitX, InitY);
  91.       XLength := InitXLength;
  92.       YLength := InitYLength;
  93.    end;
  94.  
  95.    procedure FullLine.Show;
  96.    begin
  97.       Visible := True;
  98.       Graph.Line(X,Y,X + XLength,Y + YLength);
  99.    end;
  100.  
  101.    procedure FullLine.Hide;
  102.    var
  103.       HoldColor : word;
  104.    begin
  105.       HoldColor := Graph.GetColor;
  106.       Graph.SetColor(GetBkColor);
  107.       Visible := false;
  108.       Graph.Line(X,Y,X + XLength,Y + YLength);
  109.       Graph.SetColor(HoldColor);
  110.    end;
  111.  
  112. {-------------------------------------------------------}
  113. {  Rectangle's method implementation                    }
  114. {-------------------------------------------------------}
  115.  
  116.    constructor Rectangle.Init(InitX, InitY : Integer;
  117.                              InitXLength, InitYLength : Integer);
  118.  
  119.    begin
  120.       Point.Init(InitX, InitY);
  121.       XLength := InitXLength;
  122.       YLength := InitYLength;
  123.    end;
  124.  
  125.    procedure Rectangle.Show;
  126.    begin
  127.       Visible := True;
  128.       Graph.Rectangle(X,Y,X + XLength ,Y + YLength);
  129.    end;
  130.  
  131.    procedure Rectangle.Hide;
  132.    var
  133.       HoldColor : word;
  134.    begin
  135.       HoldColor := Graph.GetColor;
  136.       Graph.SetColor(GetBkColor);
  137.       Visible := false;
  138.       Graph.Rectangle(X,Y,X + XLength ,Y + YLength);
  139.       Graph.SetColor(HoldColor);
  140.    end;
  141.  
  142. {--------------------------------------------------------}
  143. { Main program:                                          }
  144. {--------------------------------------------------------}
  145.  
  146. begin
  147.   GraphDriver := Detect; { Let the BGI determine what board
  148.                            you're using }
  149.   DetectGraph(GraphDriver, GraphMode);
  150.   InitGraph(GraphDriver, GraphMode,'');
  151.   if GraphResult <> GrOK then
  152.     begin
  153.       WriteLn('>>Halted on graphics error:',
  154.               GraphErrorMsg(GraphDriver));
  155.       Halt(1)
  156.     end;
  157.  
  158. { All descendents of type Point contain virtual methods and    }
  159. { *must* be initialized before use through a constructor call. }
  160.  
  161.   ACircle.Init(151, 82,      { Initialize X,Y at 151,82 }
  162.                50);          { Initialize radius of 50 pixels }
  163.   AnArc.Init(151, 82,        { Initialize X,Y at 151,82 }
  164.              25, 0, 90);     { Initialize radius of 25 pixels }
  165.                              { Start angle: 0; End angle: 90 }
  166.  
  167.   AFullLine.Init(100,100,200,200);
  168.   ARectangle.Init(200,200,100,100);
  169.   APoint.Init(600,300);
  170.  
  171. { Replace ARectangle with AFullLine or AnArc or ACircle to drag
  172.   a full line or an arc or a circle instead of a full line }
  173. { Press Enter to stop dragging and end the program.   }
  174.  
  175. repeat
  176.   ClearDevice;
  177.   OutTextXY(0,0,' Use arrow keys to move figure. Press the ENTER key to show each new figure,');
  178.   OutTextXY(0,20,' Rectangle, Full line, Circle, Arc, Point and to continue  ');
  179.   ARectangle.Drag(5);       { Parameter is # of pixels to drag by }
  180.   AFullLine.Drag(5);
  181.   ACircle.Drag(5);
  182.   AnArc.Drag(5);
  183.   APoint.Drag(5);
  184.   OutTextXY(50,410,'Repeat from the rectangle (y/n)? ');
  185.   Reply := readkey;
  186.   OutTextXY(320,410,Reply);
  187. until Reply <> 'y';
  188.   x1 := 480;
  189.   Repeat
  190.     OutTextXY(50,430,'Hide any figure? Use initial letter else N for next      ');
  191.     Reply := UpCase(readkey);
  192.     OutTextXY(x1,430,reply);
  193.     x1 := x1 + 20;
  194.     Case Reply of
  195.       'A' : AnArc.Hide;
  196.       'C' : ACircle.Hide;
  197.       'F' : AFullLine.Hide;
  198.       'P' : APoint.Hide;
  199.       'R' : ARectangle.Hide;
  200.      end;
  201.   Until Reply ='N';
  202.   x2 := 450;
  203.   Repeat
  204.  
  205.     OutTextXY(50,450,'Show any figure? Use initial letter, Q to quit  ');
  206.     Reply := UpCase(readkey);
  207.     OutTextXY(x2,450,reply);
  208.     x2 := x2 + 20;
  209.     Case Reply of
  210.       'A' : AnArc.Show;
  211.       'C' : ACircle.Show;
  212.       'F' : AFullLine.Show;
  213.       'P' : APoint.Show;
  214.       'R' : ARectangle.Show;
  215.     end;
  216.   Until Reply = 'Q';
  217.   OutTextXY(50,470,'Is hardcopy required (y/n)? ');
  218.   Reply := UpCase(readkey);
  219.   OutTextXY(270,470,Reply);
  220.   if Reply = 'Y' then HardCopy(4);
  221.   CloseGraph;
  222.   RestoreCRTMode;
  223. end.
  224.  
  225.  
  226.  
  227.